﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharePointUsers
{
    public partial class ReportMain : Form
    {
        public SPReport ReportData = new SPReport();
        public ReportMain()
        {
            InitializeComponent();
        }

        private void ReportMain_Load(object sender, EventArgs e)
        {
            //Prepare report settings page
            if (ReportData != null)
            {
                int Top = 0;
                //Add the settings check boxes
                int SettingsCount = 1;
                foreach (var setting in ReportData.Settings)
                {
                    CheckBox chkBoxSetting = new CheckBox();
                    chkBoxSetting.Text = setting.Key;
                    chkBoxSetting.Checked = setting.Value;
                    chkBoxSetting.AutoSize = true;
                    chkBoxSetting.Top = Top;
                    chkBoxSetting.Name = "chk" + SettingsCount.ToString();
                    Top += 20;
                    panelSettings.Controls.Add(chkBoxSetting);
                    SettingsCount++;
                }

                //Add the properties input fields
                int PropertyCount = 1;
                foreach (var properties in ReportData.Properties)
                {
                    if (properties.Value.Input)
                    {
                        Label lblProperty = new Label();
                        lblProperty.AutoSize = true;
                        lblProperty.Text = properties.Value.DisplayName;
                        lblProperty.Top = Top;
                        lblProperty.Name = "lbl" + PropertyCount.ToString();
                        
                        TextBox txtBoxProperty = new TextBox();
                        txtBoxProperty.Text = properties.Value.Value;
                        txtBoxProperty.AutoSize = true;
                        txtBoxProperty.Top = Top;
                        txtBoxProperty.Left = lblProperty.Width + 20;
                        txtBoxProperty.Name = properties.Key;
                        Top += 20;
                        PropertyCount++;
                        panelSettings.Controls.Add(lblProperty);
                        panelSettings.Controls.Add(txtBoxProperty);
                    }
                }
                this.Text = this.Text.Replace("[report]", ReportData.Name);
            }
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonCreateReport_Click(object sender, EventArgs e)
        {
            //Display warning if required
            if (ReportData.DisplayUpdateWarning)
            {
                DialogResult WarningResult = MessageBox.Show(ReportData.UpdateWarning + " Do you want to continue?",
                                    "Report action warning", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
                if (WarningResult == System.Windows.Forms.DialogResult.No)
                {
                    this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                    this.Close();
                    return;
                }
                else if (WarningResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    return;
                }

            }
            //Send user settings
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            foreach (Control control in panelSettings.Controls)
            {
                if (control is CheckBox)
                {
                    CheckBox chkboxObj = control as CheckBox;
                    if (chkboxObj != null)
                    {
                        ReportData.Settings[chkboxObj.Text] = chkboxObj.Checked;
                    }
                }
                else if (control is TextBox)
                {
                    TextBox txtboxObj = control as TextBox;
                    ReportData.Properties[txtboxObj.Name].Value = txtboxObj.Text;

                }
            }

            this.Close();
        }
    }
}
